package com.langsin.Demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
@Author: MFM
@Date: 2021/8/2 - 08 - 02 - 16:23 
@version: 3.12
*/

public class Demo {
    public static <T extends Comparable<T>> void sort(List<T> list){
        if (list.isEmpty()){
            return;
        }

        List<T> currentList = new ArrayList<>();

        while (list.size()>0){
            currentList.add(list.remove(0));
        }

        list.add(currentList.remove(currentList.size()-1));
        Stack<T> stack = new Stack<>();

        while (currentList.size() > 0) {
            T value = currentList.remove(currentList.size() - 1);
            while (list.size() > 0) {
                T lastValue = list.get(list.size() - 1);
                if (value.compareTo(lastValue) > 0) {
                    list.add(value);
                    break;
                } else {
                    stack.push(list.remove(list.size() - 1));
                }
            }
            if (list.isEmpty()) {
                list.add(value);
            }
            while (stack.size() > 0) {
                list.add(stack.pop());
            }
        }

    }

    public static void main(String[] args) {
        List<Double> list = new ArrayList<>();
        list.add(12.3);
        list.add(33.5);
        list.add(234.4);
        list.add(23.11);
        list.add(324.5543);
        sort(list);
        System.out.println(list);
    }
}
